#!/bin/bash

echo 'cinder 服务';

echo '安装软件包';
yum install openstack-cinder targetcli python-keystone -y;
# cinder 节点 lvm2 服务
echo '启动软件，并设置开机启动';
systemctl enable lvm2-lvmetad.service
systemctl start lvm2-lvmetad.service
echo 'lvm2-lvmetad.service运行状态';
echo '#######################################################';
systemctl status lvm2-lvmetad.service
echo '#######################################################';

echo '创库授权';
mysql -uroot -p123456 -e "
CREATE DATABASE cinder;
GRANT ALL PRIVILEGES ON cinder.* TO 'cinder'@'localhost' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON cinder.* TO 'cinder'@'%' IDENTIFIED BY '123456';
exit"

echo '加载环境变量。';
source /root/admin-openrc
echo '创建cinder用户。';
openstack user create --domain default --password 123456 cinder
echo '将cinder用户关联到service项目，并授予admin权限。';
openstack role add --project service --user cinder admin
echo '创建cinder相关服务。';
openstack service create --name cinderv2 --description "OpenStack Block Storage" volumev2
openstack service create --name cinderv3 --description "OpenStack Block Storage" volumev3
echo '创建endpoint。';
openstack endpoint create --region RegionOne volumev2 public http://controller:8776/v2/%\(project_id\)s
openstack endpoint create --region RegionOne volumev2 internal http://controller:8776/v2/%\(project_id\)s
openstack endpoint create --region RegionOne volumev2 admin http://controller:8776/v2/%\(project_id\)s
openstack endpoint create --region RegionOne volumev3 public http://controller:8776/v3/%\(project_id\)s
openstack endpoint create --region RegionOne volumev3 internal http://controller:8776/v3/%\(project_id\)s
openstack endpoint create --region RegionOne volumev3 admin http://controller:8776/v3/%\(project_id\)s

echo '修改配置文件';
# 主机配置my_ip
cp /etc/cinder/cinder.conf{,.bak}
grep -Ev '^$|#' /etc/cinder/cinder.conf.bak > /etc/cinder/cinder.conf
openstack-config --set /etc/cinder/cinder.conf database connection mysql+pymysql://cinder:123456@controller/cinder
openstack-config --set /etc/cinder/cinder.conf DEFAULT transport_url rabbit://openstack:123456@controller
openstack-config --set /etc/cinder/cinder.conf DEFAULT auth_strategy keystone
openstack-config --set /etc/cinder/cinder.conf DEFAULT my_ip 10.71.11.6
openstack-config --set /etc/cinder/cinder.conf DEFAULT enabled_backends lvm
openstack-config --set /etc/cinder/cinder.conf DEFAULT glance_api_servers http://controller:9292
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_uri http://controller:5000
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_url http://controller:35357
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken memcached_servers controller:11211
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_type password
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken project_domain_id default
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken user_domain_id default
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken project_name service
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken username cinder
openstack-config --set /etc/cinder/cinder.conf keystone_authtoken password 123456
openstack-config --set /etc/cinder/cinder.conf oslo_concurrency lock_path /var/lib/cinder/tmp
openstack-config --set /etc/cinder/cinder.conf lvm volume_driver cinder.volume.drivers.lvm.LVMVolumeDriver 
openstack-config --set /etc/cinder/cinder.conf lvm volume_group cinder-volumes 
openstack-config --set /etc/cinder/cinder.conf lvm iscsi_protocol iscsi 
openstack-config --set /etc/cinder/cinder.conf lvm iscsi_helper lioadm

echo '同步数据库';
su -s /bin/sh -c "cinder-manage db sync" cinder
echo '修改nova配置，使实例能使用cinder存储';
openstack-config --set /etc/nova/nova.conf cinder os_region_name RegionOne
echo '重启nova-api服务';
systemctl restart openstack-nova-api.service
# controller 节点 nova 服务
echo '           openstack-nova-api.service运行状态          ';
echo '#######################################################';
systemctl status openstack-nova-api.service
echo '#######################################################';
# cinder 节点 cinder 服务
systemctl enable openstack-cinder-volume.service target.service 
systemctl start openstack-cinder-volume.service target.service
echo 'openstack-cinder-volume.service target.service运行状态';
echo '#######################################################';
systemctl status openstack-cinder-volume.service target.service
echo '#######################################################';
# controller 节点 cinder 服务
systemctl enable openstack-cinder-api.service openstack-cinder-scheduler.service
systemctl start openstack-cinder-api.service openstack-cinder-scheduler.service
echo 'openstack-cinder-api.service openstack-cinder-scheduler.service运行状态'
echo '#######################################################';
systemctl status openstack-cinder-api.service openstack-cinder-scheduler.service
echo '#######################################################';
echo '测试cinder服务';
source /root/admin-openrc
openstack volume service list

echo '-----------------------------------------------------'
echo '                  cinder  配置完成！                '
echo '-----------------------------------------------------'